package mutilthread;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description: wait And notify
 * User: Li_yizYa
 * Date: 2025—03—25
 * Time: 17:39
 */
public class Demo11 {
    private static Object locker = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            synchronized (locker) {//加锁和wait必须是同一个对象
                System.out.println("t1 wait 之前");
                try {
                    locker.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("t1 wait 之后");
            }
        });

        Thread t2 = new Thread(() -> {
            System.out.println("t2 notify 之前");
            Scanner scan = new Scanner(System.in);
            scan.next(); //此处用户输入啥都行，主要是通过 next 来构造 “阻塞” 操作
            synchronized (locker) {
                locker.notify();//确保与 wait 是同一个对象，才能正确进行唤醒
            }
            System.out.println("t2 notify 之后");
        });

        t1.start();
        t2.start();
    }
}
